﻿<%@page import="java.util.Date"%>
<%@ page import="java.sql.Timestamp"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="com.pension.model.dto.Board"%>
<%@ page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<%!

String getTitleString(Board board) {
	
	StringBuilder html = new StringBuilder(256);
	
	//댓글 들여쓰기 처리
	for (int i = 0; i < board.getDepth(); i++) {
		html.append("&nbsp;&nbsp;");	
	}
	
	if (board.getDepth() > 0) {   // depth가 0보다 크면 댓글이다.
		html.append("<img src='/pension/image/re.gif' /> ");
	}
	
	if (board.isDeleted()) {
		html.append("<span style='color:#B1B1B1' ");
		html.append("onclick=\"alert('삭제된 글입니다.');\">");
		html.append(board.getTitle());
		html.append("&nbsp;(삭제된 글)");
		html.append("</span>");
	} else {
		html.append("<a href='javascript:viewboard(");
		html.append(board.getBoardNo());
		html.append(");' style='text-decoration: none; color: #333333;'>");
		html.append(board.getTitle()); //style="text-decoration: none; color: #333333;"
		html.append("</a>");
	}
	
	if (board.getReadCount() < 5) {  // 조회수가 5 미만은 글, 댓글은 new가 붙는다.
		html.append(" <img src='/pension/image/new.gif' />");
	}
	
	return html.toString();
}
%>
<!DOCTYPE html>

<html>
<head>
	<meta charset="utf-8" />
	<title>게시물 목록</title>
	<link rel="Stylesheet" href="/pension/styles/board/default.css" />
	<script type="text/javascript">
	function viewboard(boardno) {
		
		<%
		String pageNo = request.getParameter("pageno");
		if (pageNo == null || pageNo.length() == 0)
			pageNo = "1";
		%>
		
		<% if (session.getAttribute("loginuser") != null) { %>
		location.href = 
			'view.action?boardno=' + boardno + "&pageno=<%= pageNo %>";
		<% } else { %>
		location.href = 
			"/pension/account/loginform.action?" + 
			"returnuri=/pension/board/view.action?boardno=" + 
			boardno + "&pageno=<%= pageNo %>";
		<% } %>
	}
	</script>
</head>
<body>
	<% Date date = new Date(); %>
	<% pageContext.include("/WEB-INF/views/include/header.jsp"); %>
	
	<div id="pageContainer">
		
		<div style="padding-top:25px; text-align:center; color:#DBDBDB;">
		
			<br /><br /><br />
			<input type="button" value="글쓰기" 
				style="width:300px;height:25px"
				onclick="location.href='writeform.action';" />
			<br /><br />
			
			<% List<Board> boards = (List<Board>)request.getAttribute("boards"); %>
			<% if (boards != null && boards.size() > 0) { %>
			<table border="1" align="center">
				<tr style="background-color:#4B4B4B;height:30px">
					<th style="width:50px">번호</th>
					<th style="width:300px">제 목</th>
					<th style="width:150px">작성자</th>
					<th style="width:120px">작성일</th>
					<th style="width:80px">조회수</th>
				</tr>
				<% for (Board board : boards) { %>				
				<tr style="background-color:#808080; height:30px; color:#DBDBDB;">
					<td style="text-align:center;"><%= board.getBoardNo() %></td>
					<td style="text-align:left; padding:5px;">
<%-- 						<a href="view.action?boardno=<%= board.getBoardNo() %>";>
						<a href="javascript:viewboard(<%= board.getBoardNo() %>);" style="text-decoration: none; color: #333333;">
						<%= board.getTitle() %></a> --%>
						<%= getTitleString(board) %>
					</td>
					<td style="text-align:center"><%= board.getWriter() %></td>
					<td style="text-align:center"><%
					SimpleDateFormat dfhm = new SimpleDateFormat(
							"yyyyMMddHHmmss"); //날짜와 시간까지 계산해본다.
					SimpleDateFormat dfhm2 = new SimpleDateFormat(
							"yyyy-MM-dd");

					String resdate = dfhm.format(board.getRegDate());
					String today = dfhm.format(date.getTime());
					String orgtime = dfhm2.format(board.getRegDate());

					Date beginDate = null;
					Date endDate = null;

					long diff = 0;
					long diffDays = 0;

					String timename = "";

					long diffTimes = 0;

					//parse: 문자형 날짜 -> Date 형태로 변환
					beginDate = dfhm.parse(resdate);
					endDate = dfhm.parse(today);

					//System.out.println(resdate);
					//System.out.println(today);
					//System.out.println(s_snsupload.getS_regdate());
					//System.out.println(endDate.getTime());
					//System.out.println(beginDate.getTime());

					diff = endDate.getTime() - beginDate.getTime(); // 0.001초 단위로 계산됨

					//diffDays  = diff / (24 * 60 * 60 * 1000);

					//System.out.println(diff);

  					if (diff < 60000) {
						diffTimes = diff / 1000;
						timename = "방금 전";
						out.print(timename);
					}
  					else if (diff < (60 * 60 * 1000)) {
						diffTimes = diff / (60 * 1000);
						timename = "분 전";
						out.print(diffTimes + timename);
					} else if (diff < (24 * 60 * 60 * 1000)) {
						diffTimes = diff / (60 * 60 * 1000);
						timename = "시간 전";
						out.print(diffTimes + timename);
					} else if (diff >= (24 * 60 * 60 * 1000)){
						 diffTimes = diff / (24 * 60 * 60 * 1000);
						 timename = "일 전";
						 out.print(orgtime);
					} /* else if (diff >= (24 * 60 * 60 * 1000)){
					 diffTimes = diff / (24 * 60 * 60 * 1000);
					 timename = "일 전";
					 out.print(diffTimes + timename);
					} */
/* 					System.out.println("today : " + today + " :::: resdate : " + resdate);
					System.out.println(diffTimes + timename); */
					%></td>
					<td style="text-align:center"><%= board.getReadCount() %></td>
				</tr>
				<% } %>
			</table>
			<% } else { %>
				<div style='text-align:center'>등록된 게시물이 없습니다.</div>
			<% } %>
			<br /><br />
			
			<%= request.getAttribute("pager").toString() %>
	
		</div>
	</div>
	<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
	<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
	<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
	<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

</body>
</html>













